package cn.gailvlun.gll.widget.edittext;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.AttributeSet;

import com.scwang.smartrefresh.layout.util.DensityUtil;

import cn.gailvlun.gll.net.R;




public class ClearEditText extends RightImageEditText {


    public ClearEditText(Context context) {
        super(context);
    }

    public ClearEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ClearEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init() {
        super.init();

        addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                updateRightDrawable(s.length() > 0);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        setRightDrawable(ContextCompat.getDrawable(getContext(), R.drawable.ic_input_delete),
                DensityUtil.dp2px(24), DensityUtil.dp2px(24));
        setWhenFocusAppear(true);
        setOnRightClickListener(new OnRightClickListener() {
            @Override
            public void onRightClick(RightImageEditText riet) {
                setText(null);

            }
        });
    }


    public void startFilter(@NonNull FilterType type) {
        InputFilter filter = null;
        switch (type) {
            case NICKNAME:
                filter = new LengthFilter(30);
                break;
            case PHONE:
            case CAPTCHA:
                filter = new DigitsFilter(type);
                break;
            case INVITATION:
                filter = new InvitationFilter();
                break;
        }
        setFilters(new InputFilter[]{filter});
    }


}
